<?php
namespace Modules\Chip\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Chip\Entities\Chip;
use \Modules\Chip\Entities\ChipAsset;

use Validator;
use ThrowException;
use DB;
use Auth;

class ChipAssetController extends Controller
{
    // 确认兑换
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $allAsset = $user->chip_assets;

        $list = Chip::getList([
            'scopes' => ['onStock'],
            'only' => [
                'id', 
                'uuid',
                'title',
                'icon',
                'balance'
            ]
        ]);

        $list->each(function ($item) use ($allAsset) {
            $item->balance = @$allAsset->where('chip_id', $item->id)->first()->balance ?: 0;
        });

        return $list->generateListResponse();
    }
}